package com.kasa.zto;

import java.util.ArrayList;
import java.util.Collection;

public class ZTOArrayList<T extends ZTOType> extends ArrayList<T> implements ZTOType
{
    private static final long serialVersionUID = -2050337366389477552L;

    private Class<? extends T> elementClass;

    public ZTOArrayList(int initialCapacity, Class<? extends T> elementClass)
    {
        super(initialCapacity);
        this.elementClass = elementClass;
    }

    public ZTOArrayList(Class<? extends T> elementClass)
    {
        super();
        this.elementClass = elementClass;
    }

    public ZTOArrayList(Collection<? extends T> c, Class<? extends T> elementClass)
    {
        super(c);
        this.elementClass = elementClass;
    }

    public void encode(ZTOEncoder encoder) throws ZTOException
    {
        int size = this.size();
        encoder.encode_int(size);
        for (ZTOType type : this)
        {
            encoder.encodeZTOType(type);
        }
    }

    public void decode(ZTODecoder decoder) throws ZTOException
    {       
        int size = decoder.decode_int();
        T one;
        for (int i = 0; i < size; i++)
        {
        	one = decoder.decodeZTOType(elementClass);
			add(one);
        }
    }

    public Class<? extends ZTOType> getElementClass()
    {
        return this.elementClass;
    }
}
